package com.example.library.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.library.R;
import com.example.library.myRecyclerview.AttributeDecoration;
import com.example.library.myRecyclerview.CommonAdapter;

import com.example.library.http.KnowledgeParseEntity.CxCollectionBean.DiscCollectionBean;
import com.example.library.myRecyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.Nullable;

/**
 * Created by dadong on 2018/9/19.
 * Describe:
 */
public class NewDifferentiationFragment extends Fragment {
    View mView;
    List<DiscCollectionBean> differentiationBeans = new ArrayList<>();
    RecyclerView rvUsage;
    CommonAdapter<DiscCollectionBean> differentiationAdapter;
    LinearLayoutManager rvUsageLm;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.view_knowledge_useage, container, false);
        initView(mView);
        return mView;
    }

    private void initView(View view) {
        rvUsage = view.findViewById(R.id.rv_usage);
        rvUsageLm = new LinearLayoutManager(getContext()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        rvUsageLm.setOrientation(LinearLayoutManager.VERTICAL);
        if (differentiationBeans.size() > 0) {
            initDifferentiationAdapter();
            rvUsage.setAdapter(differentiationAdapter);
        }
        rvUsage.setLayoutManager(rvUsageLm);
        rvUsage.addItemDecoration(new AttributeDecoration(getContext(), new AttributeDecoration.DecorationCallback() {
            @Override
            public String getGroupId(int position) {
                return differentiationBeans.get(position).getCxEnglish();
            }

            @Override
            public String getGroupFirstLine(int position) {
                return differentiationBeans.get(position).getCxEnglish();
            }
        }));
    }

    private void initDifferentiationAdapter() {
        differentiationAdapter = new CommonAdapter<DiscCollectionBean>(getContext(), R.layout.view_example_item, differentiationBeans) {
            @Override
            protected void convert(ViewHolder holder, DiscCollectionBean discCollectionBean, int position) {
                holder.getView(R.id.iv_pronunciation).setVisibility(View.GONE);
                holder.setText(R.id.tv_english, Html.fromHtml(discCollectionBean.getWord()) + "");
                holder.setText(R.id.tv_chinese, Html.fromHtml(discCollectionBean.getDescription()) + "");

            }
        };
    }

    public List<DiscCollectionBean> getDifferentiationBeans() {
        return differentiationBeans;
    }

    public void setDifferentiationBeans(List<DiscCollectionBean> differentiationBeans) {
        if (this.differentiationBeans.size() > 0) {
            this.differentiationBeans.clear();
        }
        this.differentiationBeans.addAll(differentiationBeans);
    }
}
